package com.code.generator.cn.util.base;

import com.alibaba.fastjson.JSON;
import com.code.generator.cn.entity.CoderField;
import com.code.generator.cn.entity.CoderTable;
import com.code.generator.cn.util.MyFileUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class VueUtil {


    public static void createListVue(CoderTable coderTable, List<CoderField> coderFields, Configuration configuration, String uploadPath) throws IOException, TemplateException {
        Template temp=configuration.getTemplate("/code/list.ftl");
        String fieldName= "List.vue";
        createVue(coderTable, coderFields, uploadPath, temp,fieldName);
        return;

    }

    private static void createVue(CoderTable coderTable, List<CoderField> coderFields, String uploadPath, Template temp,String fileName) throws IOException, TemplateException {
        Writer file =null;

        //获取包名
        String packageName=coderTable.getPackageName();
        String groupPath=packageName.replace(".","/");
        //生成entity的路径
        String entityPath=uploadPath+"/vue/";
        //设置生成的路径
        MyFileUtil.createFileDir(entityPath);
        //设置生成的属性
        Map<String,Object> createMap=new HashMap<>();

        //设置实体对象的类名
        String className=coderTable.getModifyName();
        createMap.put("className",className);

        //设置字段属性
        List<Map<String,Object>> fieldList=new ArrayList<>();
        for (CoderField coderField : coderFields) {
            Map<String, Object> fieldMap = CommonUtil.createFieldMap(coderField,createMap);
            fieldList.add(fieldMap);
        }
        if(!createMap.containsKey("DATEIMPORT")){
            createMap.put("DATEIMPORT","");
        }
        file = new FileWriter(entityPath+className+fileName);
        createMap.put("fieldList",fieldList);
        log.info(JSON.toJSONString(createMap));
        temp.process(createMap, file);
        //设置生成的文件名
        file.close();
    }


    public static void createModalVue(CoderTable coderTable, List<CoderField> coderFields, Configuration configuration, String uploadPath) throws IOException, TemplateException {
        Template temp=configuration.getTemplate("/code/modal.ftl");
        String fieldName= "Modal.vue";
        createVue(coderTable, coderFields, uploadPath, temp,fieldName);

    }

    public static void createDrawerVue(CoderTable coderTable, List<CoderField> coderFields, Configuration configuration, String uploadPath) throws IOException, TemplateException {
        Template temp=configuration.getTemplate("/code/drawer.ftl");
        String fieldName= "Drawer.vue";
        createVue(coderTable, coderFields, uploadPath, temp,fieldName);

    }


    public static void createInfoVue(CoderTable coderTable, List<CoderField> coderFields, Configuration configuration, String uploadPath) throws IOException, TemplateException {
        Template temp=configuration.getTemplate("/code/info.ftl");
        String fieldName= "Info.vue";
        createVue(coderTable, coderFields, uploadPath, temp,fieldName);

    }
}
